package net.techwatch.jpa.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name="SALARY_HISTORY")
public class SalaryHistory {

	private long id;
	private Date changeDate;
	private Employee updater;
	private long oldSalary;
	private int percentChange;
	
	public SalaryHistory() {
		// nothing to do
	}

	/**
	 * @return the id
	 */
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="SALARY_HISTORY_ID")
	public long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	private void setId(long id) {
		this.id = id;
	}

	/**
	 * @return the changeDate
	 */
	@Temporal(TemporalType.DATE)
	@Column(name="CHANGE_DATE", nullable=false)
	public Date getChangeDate() {
		return changeDate;
	}

	/**
	 * @param changeDate the changeDate to set
	 */
	public void setChangeDate(Date changeDate) {
		this.changeDate = changeDate;
	}

	/**
	 * @return the updaterId
	 */
	@OneToOne
	@JoinColumn(name="UPDATER", nullable=false, referencedColumnName="PERSON_ID")
	public Employee getUpdater() {
		return updater;
	}

	/**
	 * @param updaterId the updaterId to set
	 */
	public void setUpdater(Employee updater) {
		this.updater = updater;
	}

	/**
	 * @return the oldSalary
	 */
	@Column(name="OLD_SALARY", nullable=false)
	public long getOldSalary() {
		return oldSalary;
	}

	/**
	 * @param oldSalary the oldSalary to set
	 */
	public void setOldSalary(long oldSalary) {
		this.oldSalary = oldSalary;
	}

	/**
	 * @return the percentchange
	 */
	@Column(name="PERCENT_CHANGE", nullable=false)
	public int getPercentChange() {
		return percentChange;
	}

	/**
	 * @param percentchange the percentchange to set
	 */
	public void setPercentChange(int percentchange) {
		this.percentChange = percentchange;
	}

	/**
	 * @return the newSalary
	 */
	@Transient
	public long getNewSalary() {
		return oldSalary * (1l + percentChange / 100l);
	}

	/**
	 * @param newSalary the newSalary to set
	 */
	private void setNewSalary(long newSalary) {
		// nothing to do: transient
	}
}
